<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiListModel;
use app\index\model\ContractShiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginXunModel;
use app\index\model\EnginZhengModel;
use app\index\model\FlowBillModel;
use app\index\model\IncomeEnginModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouListModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffJieModel;
use app\index\model\SubbaoShenModel;
use app\index\model\UserModel;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\EnginService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Ban extends Common
{

    public function zi_zong(Request $request){
        $param = $request->post();
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        $whereChan = [];
        if($chanid_pid > 0){
            $whereChan[] = ['e.chanid','in', $chanid_son];
        }

        $whereName = [];
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereName[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }

        $whereFu = [];
        if(isset($param['man_xiang']) && !empty($param['man_xiang'])){
            $whereFu[] = ['man.realname','like', '%'.$param['man_xiang'].'%'];
        }

        $whereEnginType = [];
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $whereEnginType[] = ['e.engintype','=', $param['engintype']];
        }


        $topArr = [];
        $topArr['zai'] = 0;

        $enginIdArr = [0];
        $enginInfo = EnginModel::alias('e')
            ->field('e.*')
            ->leftJoin('user man','man.id=e.man_xiang')
            ->where(['e.deleted_user'=>0,'e.comid'=>$this->userinfo['comid']])
            ->where(['e.status'=>1,'e.isturn'=>1,'e.business'=>1])
            ->where($whereChan)->where($whereName)->where($whereFu)->where($whereEnginType)
            ->select()->toArray();
        foreach ($enginInfo as $vvv){
            $enginIdArr[] = $vvv['id'];
            if($vvv['state'] == 3){
                $topArr['zai'] += 1;
            }
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';
        $data['enginidstr'] = implode(',', $enginIdArr);

        $yeararr = [];
        for ($i=date('Y'); $i>date('Y')-10; $i--){
            $yeararr[] = (int)$i;
        }
        $data['yearjin']  = date('Y');
        $data['yeararr']  = $yeararr;




        $topArr['shimoney'] = 0;
        $shiinfo = ContractShiModel::field('id,money')->where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->select()->toArray();
        foreach ($shiinfo as $vv){
            $topArr['shimoney'] += $vv['money'];
            $topArr['shimoney'] += ContractBuModel::where($where)->where($whereTurn)->where(['shiid'=>$vv['id']])->sum('money');
        }

        $topArr['chan'] = EnginChanModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('leijin');

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);
        $topArr['huikuan'] = IncomeEnginModel::where($where)->where('enginid','in',$enginIdArr)
            ->where('enginid','>',0)
            ->where('budid','in',$budshou)->sum('money');


        $xian_daofee = IncomeEnginModel::where($where)->where('enginid','in',$enginIdArr)
            ->where('enginid','>',0)->sum('money');
        $payenginId = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->column('id');
        $xian_zhifee =  AccountListModel::where(['type'=>3])->where('mid','in',$payenginId)->sum('money');
        $baoId = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->column('id');
        $xian_zhifee +=  AccountListModel::where(['type'=>4])->where('mid','in',$baoId)->sum('money');
        $neifuId = PaymentNeifuModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->column('id');
        $xian_zhifee +=  AccountListModel::where(['type'=>6])->where('mid','in',$neifuId)->sum('money');
        $topArr['yufee'] = round($xian_daofee - $xian_zhifee, 2);

        $data['top'] = $topArr;




        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_dongli(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $data['kaichan'] = EnginChanModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('leijin');
        $data['shicheng'] = StuffJieModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['shicheng'] += LabourShenModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['shicheng'] += SubbaoShenModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['shicheng'] += DeviceJieModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['shicheng'] += LabourLingModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['shicheng'] += DeviceLinModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['shicheng'] += PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('shenfee');
        $data['run'] = round($data['kaichan'] - $data['shicheng'], 2);



        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_fashui(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $data['xiaofa'] = PiaokaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');
        $data['jinfa'] = PiaoShouModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['xiaoshui'] =  PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where('zhu.enginid','in',$enginidstr)
            ->sum('list.ratefee');
        $data['jinshui'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where('zhu.enginid','in',$enginidstr)
            ->where(['list.piaotype'=>1])
            ->sum('list.ratefee');

        $data['zengshui'] = round($data['xiaoshui'] - $data['jinshui'], 2);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_zhian(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];



        $data['xun'] = EnginXunModel::where($where)->where('enginid','in',$enginidstr)->count();
        $data['zheng'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr)->count();
        $data['yigai'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr) ->where(['gaista'=>1])->count();
        $data['daigai'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr) ->where(['gaista'=>0])->count();
        $data['daigaiyu'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr) ->where(['gaista'=>0])
            ->where('dt', '<', date("Y-m-d"))->count();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_baojin(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $data['jiao'] = EnginBondJiaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $jiaoId = EnginBondJiaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $data['tui'] =  AccountListModel::where(['type'=>8])->where('mid','in',$jiaoId)->sum('money');

        $data['dai'] = round($data['jiao'] - $data['tui'], 2);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_zaxiang(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $data['xian_daofee'] = IncomeEnginModel::where($where)->where('enginid','>',0)->where('enginid','in',$enginidstr)->sum('money');
        $payenginId = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $data['xian_zhifee'] =  AccountListModel::where(['type'=>3])->where('mid','in',$payenginId)->sum('money');
        $baoId = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $data['xian_zhifee'] +=  AccountListModel::where(['type'=>4])->where('mid','in',$baoId)->sum('money');
        $neifuId = PaymentNeifuModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $data['xian_zhifee'] +=  AccountListModel::where(['type'=>6])->where('mid','in',$neifuId)->sum('money');
        $data['za_yufee'] = round($data['xian_daofee'] - $data['xian_zhifee'], 2);

        $data['za_chan'] = EnginChanModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('leijin');

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);
        $data['za_huikuan'] = IncomeEnginModel::where($where)->where('enginid','>',0)->where('enginid','in',$enginidstr)->where('budid','in',$budshou)->sum('money');

        $data['za_caimoney'] = ContractCaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');

        $data['za_xiangzhi'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['za_xiangzhi'] += PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');


        $data['za_zhicheng'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where('budid','in',[1,2,3,4,5])->sum('money');
        $data['za_guancheng'] = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('shenfee');

        $data['za_zongcheng'] = round($data['za_zhicheng'] + $data['za_guancheng'], 2);

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_zifen(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $shou = [];
        $zhi = [];
        $cha = [];

        $inenArr = IncomeEnginModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where($where)->where('enginid','in',$enginidstr)->where('enginid','>',0)
            ->select()->toArray();


        $acArr = [];
        $payenginId = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $payenAc =  AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")->
        where(['type'=>3])->where('mid','in',$payenginId)->select()->toArray();

        $baoId = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $paybaoAc =  AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")->
        where(['type'=>4])->where('mid','in',$baoId)->select()->toArray();

        $neifuId = PaymentNeifuModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $neifuAc =  AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")->
        where(['type'=>6])->where('mid','in',$neifuId)->select()->toArray();

        $acArr = array_merge($acArr, $payenAc, $paybaoAc, $neifuAc);



        for ($i=1; $i<=12; $i++){
            if(!isset($shou[$i-1])){
                $shou[$i-1] = 0;
            }
            if(!isset($zhi[$i-1])){
                $zhi[$i-1] = 0;
            }
            if(!isset($cha[$i-1])){
                $cha[$i-1] = 0;
            }

            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($inenArr as $v){
                if($v['dt'] == $dt){
                    $shou[$i-1] += $v['money'];
                }
            }

            foreach ($acArr as $v){
                if($v['dt'] == $dt){
                    $zhi[$i-1] += $v['money'];
                }
            }

            $cha[$i-1] = round($shou[$i-1] - $zhi[$i-1], 2);
        }

        $data['shou'] = $shou;
        $data['zhi'] = $zhi;
        $data['cha'] = $cha;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_chanhe(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $chanList = EnginChanModel::field('month,leijin')->where($where)->where($whereTurn)
            ->where('enginid','in',$enginidstr)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['month'] == $dt){
                    $info[$i-1] += $v['leijin'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_huihe(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['comid','=',$this->userinfo['comid']];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);

        $accountlist = AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where(['type'=>1])->where('budid','in',$budshou)->where($where)->select()->toArray();


        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($accountlist as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_yingxian(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info['shimoney'] = 0;
        $shiinfo = ContractShiModel::field('id,money')->where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->select()->toArray();
        foreach ($shiinfo as $vv){
            $info['shimoney'] += $vv['money'];
            $info['shimoney'] += ContractBuModel::where($where)->where($whereTurn)->where(['shiid'=>$vv['id']])->sum('money');
        }

        $info['chan'] = EnginChanModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('leijin');
        $info['xiaofa'] = PiaokaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);
        $info['huikuan'] = IncomeEnginModel::where($where)->where('enginid','in',$enginidstr)
            ->where('enginid','>',0)
            ->where('budid','in',$budshou)->sum('money');


        $info['noshou_he'] = round($info['shimoney'] - $info['huikuan'], 2);
        $info['noshou_chan'] = round($info['chan'] - $info['huikuan'], 2);
        $info['noshou_kai'] = round($info['xiaofa'] - $info['huikuan'], 2);

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_hecai(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info = [];
        $cai = ContractCaiModel::field('caihetype,sum(money) as money')->where($where)->where($whereTurn)
            ->where('enginid','in',$enginidstr)->group('caihetype')->select()->toArray();
        foreach ($cai as $vvv){
            $info[$vvv['caihetype']] = $vvv['money'];
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_xiangzhi(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];

        $payengin = PaymentEnginModel::field("money,DATE_FORMAT(created_at, '%Y-%m') AS dt")
            ->where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->select()->toArray();
        $paybao = PaymentBaoModel::field("money,DATE_FORMAT(created_at, '%Y-%m') AS dt")
            ->where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($payengin as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }

            foreach ($paybao as $vv){
                if($vv['dt'] == $dt){
                    $info[$i-1] += $vv['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_fuxian(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info['caimoney'] = ContractCaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');

        $info['leijie'] = StuffJieModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['leijie'] += LabourShenModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['leijie'] += SubbaoShenModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['leijie'] += DeviceJieModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['leijie'] += LabourLingModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['leijie'] += DeviceLinModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');

        $info['piaoshou'] = PiaoShouModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['payengin'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $info['paybao'] = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('shenfee');


        $info['nofu_jie'] = round($info['leijie'] - $info['payengin'], 2);
        $info['nofu_shou'] = round($info['piaoshou'] - $info['payengin'] - $info['paybao'], 2);

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_zongcheng(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info = [];

        $info['cai_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>1])->sum('money');
        $info['ren_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>2])->sum('money');
        $info['zu_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>3])->sum('money');
        $info['fen_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>4])->sum('money');
        $info['cuo_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>5])->sum('money');

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zi_chart_guancheng(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $baolist = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->select()->toArray();
        $topid = EnginBudsubModel::where(['name'=>'项目费用'])->value('id');
        $kemu = EnginBudsubModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1,'topid'=>$topid])->select()->toArray();


        $nameArr = [];
        $valueArr = [];
        foreach ($kemu as $k=>&$v){
            $nameArr[] = $v['name'];
            $valueArr[$k] = 0;
            $son = (new BudgetService())->get_budsub_sonsss($v['id']);

            foreach ($baolist as $bao){
                if($bao['budid'] == $v['id']){
                    $valueArr[$k] += $bao['shenfee'];
                }else{
                    if(in_array($bao['budid'], $son)){
                        $valueArr[$k] += $bao['shenfee'];
                    }
                }
            }
        }

        $data['name'] = $nameArr;
        $data['value'] = $valueArr;
        return outputJson(2000,lang('code_2000'),$data);
    }





    public function lian_zong(Request $request){
        $param = $request->post();
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        $whereChan = [];
        if($chanid_pid > 0){
            $whereChan[] = ['e.chanid','in', $chanid_son];
        }

        $whereName = [];
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereName[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }

        $whereFu = [];
        if(isset($param['man_xiang']) && !empty($param['man_xiang'])){
            $whereFu[] = ['man.realname','like', '%'.$param['man_xiang'].'%'];
        }



        $topArr = [];
        $topArr['zai'] = 0;

        $enginIdArr = [0];
        $enginInfo = EnginModel::alias('e')
            ->field('e.*')
            ->leftJoin('user man','man.id=e.man_xiang')
            ->where(['e.deleted_user'=>0,'e.comid'=>$this->userinfo['comid']])
            ->where(['e.status'=>1,'e.isturn'=>1,'e.business'=>2])
            ->where($whereChan)->where($whereName)->where($whereFu)
            ->select()->toArray();
        foreach ($enginInfo as $vvv){
            $enginIdArr[] = $vvv['id'];
            if($vvv['state'] == 3){
                $topArr['zai'] += 1;
            }
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        $data['enginidstr'] = implode(',', $enginIdArr);

        $yeararr = [];
        for ($i=date('Y'); $i>date('Y')-10; $i--){
            $yeararr[] = (int)$i;
        }
        $data['yearjin']  = date('Y');
        $data['yeararr']  = $yeararr;




        $topArr['shimoney'] = 0;
        $shiinfo = ContractShiModel::field('id,money')->where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->select()->toArray();
        foreach ($shiinfo as $vv){
            $topArr['shimoney'] += $vv['money'];
            $topArr['shimoney'] += ContractBuModel::where($where)->where($whereTurn)->where(['shiid'=>$vv['id']])->sum('money');
        }

        $topArr['piaokai'] = PiaokaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

        $topArr['piaoshou'] = PiaoShouModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');

        $xian_daofee = IncomeEnginModel::where($where)->where('enginid','in',$enginIdArr)
            ->where('enginid','>',0)->sum('money');
        $payenginId = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->column('id');
        $xian_zhifee =  AccountListModel::where(['type'=>3])->where('mid','in',$payenginId)->sum('money');
        $baoId = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->column('id');
        $xian_zhifee +=  AccountListModel::where(['type'=>4])->where('mid','in',$baoId)->sum('money');
        $neifuId = PaymentNeifuModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->column('id');
        $xian_zhifee +=  AccountListModel::where(['type'=>6])->where('mid','in',$neifuId)->sum('money');
        $topArr['yufee'] = round($xian_daofee - $xian_zhifee, 2);

        $data['top'] = $topArr;


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_koushou(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info = [];
        $koulist = PaymentNeikouListModel::alias('list')
            ->field('list.listid,list.money')
            ->join('payment_neikou kou','kou.id=list.kouid')
            ->where(['kou.comid'=>$this->userinfo['comid'], 'kou.deleted_user'=>0])
            ->where(['kou.status'=>1, 'kou.isturn'=>1])
            ->where('kou.enginid','in',$enginidstr)->select()->toArray();


        foreach ($koulist as $vvv){
            $feiname = '';
            $neilistinfo = ContractNeiListModel::where(['id'=>$vvv['listid']])->find();
            if($neilistinfo){
                $feiname = (new OptService())->get_base_showname('engin_nei_feiname', $neilistinfo['feiname']);
            }
            $feiname = empty($feiname) ? '未知' : $feiname;

            if(!isset($info[$feiname])){
                $info[$feiname] = $vvv['money'];
            }else{
                $info[$feiname] += $vvv['money'];
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_zhian(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];



        $data['xun'] = EnginXunModel::where($where)->where('enginid','in',$enginidstr)->count();
        $data['zheng'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr)->count();
        $data['yigai'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr) ->where(['gaista'=>1])->count();
        $data['daigai'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr) ->where(['gaista'=>0])->count();
        $data['daigaiyu'] = EnginZhengModel::where($where)->where('enginid','in',$enginidstr) ->where(['gaista'=>0])
            ->where('dt', '<', date("Y-m-d"))->count();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_piaokai(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $chanList = PiaokaiModel::field("money,DATE_FORMAT(created_at, '%Y-%m') AS dt")
            ->where($where)->where($whereTurn)
            ->where('enginid','in',$enginidstr)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_piaoshou(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $chanList = PiaoShouModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where($where)->where($whereTurn)
            ->where('enginid','in',$enginidstr)->where('enginid','>',0)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_zongcheng(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info = [];

        $info['cai_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>1])->sum('money');
        $info['ren_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>2])->sum('money');
        $info['zu_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>3])->sum('money');
        $info['fen_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>4])->sum('money');
        $info['cuo_fuku'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where(['budid'=>5])->sum('money');

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_xiangshou(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $chanList = IncomeEnginModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where($where)->where('enginid','in',$enginidstr)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_zhigong(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $chanList = PaymentEnginModel::field("money,DATE_FORMAT(created_at, '%Y-%m') AS dt")
            ->where($where)->where($whereTurn)
            ->where('enginid','in',$enginidstr)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_zhilian(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];
        $year = $request->param('year', date('Y'), 'int');

        $info = [];


        $chanList = PaymentNeifuModel::field("money,DATE_FORMAT(created_at, '%Y-%m') AS dt")
            ->where($where)->where($whereTurn)
            ->where('enginid','in',$enginidstr)
            ->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
            }
        }

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_chart_baojin(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $info = [];

        $info['shou'] = EnginBondShouModel::where($where)->where('enginid','in',$enginidstr)->sum('money');

        $shouId = EnginBondShouModel::where($where)->where('enginid','in',$enginidstr)->column('id');
        $info['fan'] = EnginBondFanModel::where($where)->where($whereTurn)->where('shouid','in',$shouId)->sum('money');

        $info['jiao'] = EnginBondJiaoModel::where($where)->where('enginid','in',$enginidstr)->sum('money');

        $jiaoId = EnginBondJiaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->column('id');
        $info['tui'] =  AccountListModel::where(['type'=>8])->where('mid','in',$jiaoId)->sum('money');


        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_zaxiang(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $data['za_piaokaihe'] = PiaokaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

        $data['za_piaoshouhe'] = PiaoShouModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');

        $data['za_piao_xiao'] =  PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where('zhu.enginid','in',$enginidstr)
            ->sum('list.ratefee');

        $data['za_piao_jin'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where('zhu.enginid','in',$enginidstr)
            ->where(['list.piaotype'=>1])
            ->sum('list.ratefee');

        $data['za_piao_ying'] = round($data['za_piao_xiao'] - $data['za_piao_jin'], 2);

        $data['za_zongcheng'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->where('budid','in',[1,2,3,4,5])->sum('money');

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);
        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $dzkshou = (new BudgetService())->get_budshou_sons($dzkid);
        $budshou = array_merge($gckshou, $dzkshou);
        $data['za_huikuan'] = IncomeEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->where('enginid','>',0)->where('budid','in',$budshou)->sum('money');

        $data['za_payment_engin'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['za_payment_neifu'] = PaymentNeifuModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('money');
        $data['za_payment_neikou'] = PaymentNeikouModel::where($where)->where('enginid','in',$enginidstr)->sum('money');

        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);
        $data['za_dian'] = IncomeEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)
            ->where('enginid','>',0)->where('budid','in',$budshouDian)->sum('money');

        $data['za_payment_bao'] = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginidstr)->sum('shenfee');
        $data['za_balance'] = $data['za_huikuan'] - $data['za_payment_engin'] - $data['za_payment_bao']-
            $data['za_payment_neikou']- $data['za_payment_neifu'];

        return outputJson(2000,lang('code_2000'),$data);
    }




    public function cai_zong(Request $request){
        $param = $request->post();
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];



        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];

        $topArr['shou'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])
            ->where('type','in',[1,2,8,9,11,14])->where('dt','between',$yearDate)->sum('money');

        $topArr['zhi'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])
            ->where('type','not in',[1,2,8,9,11,14])->where('dt','between',$yearDate)->sum('money');

        $topArr['xian'] = round($topArr['shou'] - $topArr['zhi'], 2);

        $data['top'] = $topArr;


        $yeararr = [];
        for ($i=date('Y'); $i>date('Y')-10; $i--){
            $yeararr[] = (int)$i;
        }
        $data['yearjin']  = $year;
        $data['yeararr']  = $yeararr;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_zhushou(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];

        $info = [];
        $count = 0;
        $chanList = AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt', 'between', $yearDate)
            ->where('type','=',1)->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
                $count += $v['money'];
            }
        }

        $data['info'] = $info;
        $data['count'] = $count;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_zhuzhi(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];

        $info = [];
        $count = 0;
        $chanList = AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where('type','in',[3,4])->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
                $count += $v['money'];
            }
        }

        $data['info'] = $info;
        $data['count'] = $count;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_zhuzhifen(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];

        $info = [
            1 => [ 'name'=> '材料费', 'money'=>0],
            2 => [ 'name'=> '人工费', 'money'=>0],
            3 => [ 'name'=> '机械费', 'money'=>0],
            4 => [ 'name'=> '分包费', 'money'=>0],
            5 => [ 'name'=> '措施费', 'money'=>0],
            6 => [ 'name'=> '项目费用', 'money'=>0],
        ];
        $count = 0;

        $info[1]['money'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>3,'budid'=>1])->sum('money');
        $info[2]['money'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>2,'budid'=>1])->sum('money');
        $info[3]['money'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>3,'budid'=>1])->sum('money');
        $info[4]['money'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>4,'budid'=>1])->sum('money');
        $info[5]['money'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>5,'budid'=>1])->sum('money');
        $info[6]['money'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>4])->sum('money');

        $count = round($info[1]['money'] + $info[2]['money'] + $info[3]['money'] +
            $info[4]['money'] + $info[5]['money'] + $info[6]['money'], 2);


        $data['info'] = $info;
        $data['count'] = $count;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_xianfen(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];


        $shou = [];
        $zhi = [];
        $cha = [];


        $shouArr = AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt', 'between', $yearDate)
            ->where('type','in',[1,2,8,9,11,14])->select()->toArray();

        $zhiArr = AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt', 'between', $yearDate)
            ->where('type','not in',[1,2,8,9,11,14])->select()->toArray();

        for ($i=1; $i<=12; $i++){
            if(!isset($shou[$i-1])){
                $shou[$i-1] = 0;
            }
            if(!isset($zhi[$i-1])){
                $zhi[$i-1] = 0;
            }
            if(!isset($cha[$i-1])){
                $cha[$i-1] = 0;
            }

            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($shouArr as $v){
                if($v['dt'] == $dt){
                    $shou[$i-1] += $v['money'];
                }
            }

            foreach ($zhiArr as $v){
                if($v['dt'] == $dt){
                    $zhi[$i-1] += $v['money'];
                }
            }

            $cha[$i-1] = round($shou[$i-1] - $zhi[$i-1], 2);
        }

        $data['shou'] = $shou;
        $data['zhi'] = $zhi;
        $data['cha'] = $cha;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_qishou(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];


        $shouList = AccountListModel::field("money,budid")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt', 'between', $yearDate)
            ->where('type','in',[1,2,8,9,11,14])->select()->toArray();

        $topid = EnginBudshouModel::where(['name'=>'其他收入','pid'=>0])->value('id');
        $info = EnginBudshouModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1,'pid'=>$topid])->select()->toArray();

        $nameArr = [];
        $valueArr = [];

        foreach ($info as &$v){
            $v['money'] = 0;
            $son = (new BudgetService())->get_budshou_sons($v['id']);

            foreach ($shouList as $vvv){
                if($vvv['budid'] == $v['id']){
                    $v['money'] += $vvv['money'];
                }else{
                    if(in_array($vvv['budid'], $son)){
                        $v['money'] += $vvv['money'];
                    }
                }
            }
            $nameArr[] = $v['name'];
            $valueArr[] = $v['money'];
        }

        $data['nameArr'] = $nameArr;
        $data['valueArr'] = $valueArr;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_qizhi(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];

        $info = [];
        $count = 0;
        $chanList = AccountListModel::field("money,DATE_FORMAT(dt, '%Y-%m') AS dt")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt', 'between', $yearDate)
            ->where('type','in',[5,7,10,12,13,15])->select()->toArray();


        for ($i=1; $i<=12; $i++){
            if(!isset($info[$i-1])){
                $info[$i-1] = 0;
            }
            $dt = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

            foreach ($chanList as $v){
                if($v['dt'] == $dt){
                    $info[$i-1] += $v['money'];
                }
                $count += $v['money'];
            }
        }

        $data['info'] = $info;
        $data['count'] = $count;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_rizhi(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];


        $shouList = AccountListModel::field("money,budid")
            ->where(['comid'=>$this->userinfo['comid']])->where('dt', 'between', $yearDate)
            ->where('type','in',[5,7,10,12,13,15])->select()->toArray();


        $topid = EnginBudsubModel::where(['name'=>'公司费用','pid'=>0])->value('id');
        $info = EnginBudsubModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1,'pid'=>$topid])->select()->toArray();

        $nameArr = [];
        $valueArr = [];

        foreach ($info as &$v){
            $v['money'] = 0;
            $son = (new BudgetService())->get_budsub_sonsss($v['id']);

            foreach ($shouList as $vvv){
                if($vvv['budid'] == $v['id']){
                    $v['money'] += $vvv['money'];
                }else{
                    if(in_array($vvv['budid'], $son)){
                        $v['money'] += $vvv['money'];
                    }
                }
            }
            $nameArr[] = $v['name'];
            $valueArr[] = $v['money'];
        }

        $data['nameArr'] = $nameArr;
        $data['valueArr'] = $valueArr;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_chart_baojin(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $year = $request->param('year', date('Y'), 'int');
        $yearDate = [$year.'-01-01', $year.'-12-31'];
        $yearDateTime = [$year.'-01-01 00:00:00', $year.'-12-31 23:59:59'];


        $info = [];

        $info['shou'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>9])->sum('money');

        $info['jiao'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>7])->sum('money');

        $info['tui'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>8])->sum('money');

        $info['fan'] = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('dt','between',$yearDate)
            ->where(['type'=>10])->sum('money');

        $data['info'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
















}